// 多语言配置
const translations = {
    zh: {
        // 导航
        nav_home: "首页",
        nav_about: "关于我",
        nav_skills: "技能",
        nav_projects: "项目",
        nav_contact: "联系我",
        
        // 首页
        hero_title: "你好，我是",
        hero_name: "张三",
        hero_tagline: "Python Developer | 消息中间件专家",
        hero_description: "专注于实时数据处理、高可用架构设计和分布式系统开发。擅长Python、MongoDB、ElasticSearch等技术。",
        btn_projects: "查看我的项目",
        btn_contact: "联系我",
        
        // 关于我
        about_title: "关于我",
        about_role: "Python Developer & 架构师",
        about_description: "我是一名Python Developer，擅长消息中间件、实时数据处理、高可用架构设计。拥有丰富的后端开发和系统架构经验。",
        about_interests: "我对新技术充满热情，喜欢解决复杂问题。业余时间喜欢Rust编程，也是动漫爱好者，喜欢幸运星、孤独摇滚、摇曳露营等作品。",
        about_email: "zhangsan@example.com",
        about_phone: "+86 123 4567 8901",
        about_location: "中国北京市",
        timeline_title: "专业经历",
        timeline_1_year: "2010-2014",
        timeline_1_title: "清华大学",
        timeline_1_desc: "计算机科学与技术专业，本科",
        timeline_2_year: "2014-2016",
        timeline_2_title: "斯坦福大学",
        timeline_2_desc: "计算机科学专业，硕士",
        timeline_3_year: "2017-2020",
        timeline_3_title: "Google",
        timeline_3_desc: "软件工程师，负责分布式系统开发",
        timeline_4_year: "2021-2023",
        timeline_4_title: "OpenAI",
        timeline_4_desc: "软件工程师，专注于AI基础设施和实时数据处理",
        achievements_title: "个人成就",
        achievement_1_title: "清华大学优秀毕业生",
        achievement_1_desc: "学术成绩优异，综合能力突出",
        achievement_2_title: "微软MVP",
        achievement_2_desc: "Most Valuable Professional 技术专家认证",
        achievement_3_title: "华为开发者认证",
        achievement_3_desc: "高级开发者认证，专业技术认可",
        
        // 技能
        skills_title: "我的技能",
        core_skills: "核心技术",
        professional_skills: "专业技能",
        skill_python: "Python",
        skill_mongodb: "MongoDB",
        skill_nextjs: "Next.js",
        skill_elasticsearch: "ElasticSearch",
        skill_rust: "Rust",
        skill_middleware: "消息中间件",
        skill_realtime: "实时数据处理",
        skill_architecture: "高可用架构设计",
        skill_distributed: "分布式系统",
        
        // 项目
        projects_title: "我的项目",
        project_1_title: "电商网站",
        project_1_desc: "一个全功能的电商网站，包含商品展示、购物车、支付等功能。",
        project_2_title: "任务管理系统",
        project_2_desc: "一个企业级任务管理系统，支持团队协作和进度跟踪。",
        project_3_title: "数据分析平台",
        project_3_desc: "一个数据可视化平台，支持多种图表展示和数据分析功能。",
        btn_view_details: "查看详情",
        btn_source_code: "源码",
        
        // 联系
        contact_title: "联系我",
        contact_subtitle: "让我们聊聊",
        contact_description: "如果您有任何项目合作或工作机会，欢迎随时联系我。",
        contact_email: "kirineko@qq.com",
        contact_form_name: "姓名",
        contact_form_email: "邮箱",
        contact_form_message: "留言",
        contact_form_name_placeholder: "请输入您的姓名",
        contact_form_email_placeholder: "请输入您的邮箱地址",
        contact_form_message_placeholder: "请输入您想要说的话...",
        contact_form_submit: "发送消息",
        social_homepage: "个人网页",
        social_email: "邮箱",
        
        // 页脚
        footer_copyright: "© 2025 张三. 保留所有权利。"
    },
    en: {
        // 导航
        nav_home: "Home",
        nav_about: "About",
        nav_skills: "Skills",
        nav_projects: "Projects",
        nav_contact: "Contact",
        
        // 主题切换
        theme_light: "Switch to Light Mode",
        theme_dark: "Switch to Dark Mode",
        
        // Hero
        hero_title: "Hello, I'm",
        hero_name: "Zhang San",
        hero_tagline: "Python Developer | Message Middleware Expert",
        hero_description: "Focused on real-time data processing, high-availability architecture design, and distributed system development. Proficient in Python, MongoDB, ElasticSearch and other technologies.",
        btn_projects: "View My Projects",
        btn_contact: "Contact Me",
        
        // About
        about_title: "About Me",
        about_role: "Python Developer & Architect",
        about_description: "I am a Python Developer specializing in message middleware, real-time data processing, and high-availability architecture design. I have extensive experience in backend development and system architecture.",
        about_interests: "I'm passionate about new technologies and love solving complex problems. In my spare time, I enjoy Rust programming and am also an anime enthusiast, fond of works like Lucky Star, Bocchi the Rock, and Yuru Camp.",
        about_email: "zhangsan@example.com",
        about_phone: "+86 123 4567 8901",
        about_location: "Beijing, China",
        timeline_title: "Professional Experience",
        timeline_1_year: "2010-2014",
        timeline_1_title: "Tsinghua University",
        timeline_1_desc: "Bachelor's degree in Computer Science and Technology",
        timeline_2_year: "2014-2016",
        timeline_2_title: "Stanford University",
        timeline_2_desc: "Master's degree in Computer Science",
        timeline_3_year: "2017-2020",
        timeline_3_title: "Google",
        timeline_3_desc: "Software Engineer, responsible for distributed system development",
        timeline_4_year: "2021-2023",
        timeline_4_title: "OpenAI",
        timeline_4_desc: "Software Engineer, focused on AI infrastructure and real-time data processing",
        achievements_title: "Achievements",
        achievement_1_title: "Tsinghua University Outstanding Graduate",
        achievement_1_desc: "Excellent academic performance and comprehensive abilities",
        achievement_2_title: "Microsoft MVP",
        achievement_2_desc: "Most Valuable Professional technical expert certification",
        achievement_3_title: "Huawei Developer Certification",
        achievement_3_desc: "Advanced developer certification, professional technical recognition",
        
        // Skills
        skills_title: "My Skills",
        core_skills: "Core Technologies",
        professional_skills: "Professional Skills",
        skill_python: "Python",
        skill_mongodb: "MongoDB",
        skill_nextjs: "Next.js",
        skill_elasticsearch: "ElasticSearch",
        skill_rust: "Rust",
        skill_middleware: "Message Middleware",
        skill_realtime: "Real-time Data Processing",
        skill_architecture: "High-availability Architecture",
        skill_distributed: "Distributed Systems",
        
        // Projects
        projects_title: "My Projects",
        project_1_title: "E-commerce Website",
        project_1_desc: "A full-featured e-commerce website with product display, shopping cart, payment and other functions.",
        project_2_title: "Task Management System",
        project_2_desc: "An enterprise-level task management system supporting team collaboration and progress tracking.",
        project_3_title: "Data Analysis Platform",
        project_3_desc: "A data visualization platform supporting multiple chart displays and data analysis functions.",
        btn_view_details: "View Details",
        btn_source_code: "Source Code",
        
        // Contact
        contact_title: "Contact Me",
        contact_subtitle: "Let's Talk",
        contact_description: "If you have any project collaboration or job opportunities, please feel free to contact me.",
        contact_email: "kirineko@qq.com",
        contact_form_name: "Name",
        contact_form_email: "Email",
        contact_form_message: "Message",
        contact_form_name_placeholder: "Please enter your name",
        contact_form_email_placeholder: "Please enter your email address",
        contact_form_message_placeholder: "Please enter what you want to say...",
        contact_form_submit: "Send Message",
        social_homepage: "Homepage",
        social_email: "Email",
        
        // Footer
        footer_copyright: "© 2025 Zhang San. All rights reserved."
    }
};

// 语言管理器
class LanguageManager {
    constructor() {
        this.currentLanguage = localStorage.getItem('language') || 'zh';
        this.elements = {};
        this.init();
    }
    
    init() {
        this.bindElements();
        this.bindEvents();
        this.updateLanguage(this.currentLanguage);
        this.updateLanguageSelector();
    }
    
    bindElements() {
        // 获取所有需要翻译的元素
        this.elements = {
            // 导航
            nav_home: document.querySelector('nav a[href="#home"]'),
            nav_about: document.querySelector('nav a[href="#about"]'),
            nav_skills: document.querySelector('nav a[href="#skills"]'),
            nav_projects: document.querySelector('nav a[href="#projects"]'),
            nav_contact: document.querySelector('nav a[href="#contact"]'),
            
            // 首页
            hero_title: document.querySelector('.hero-text h2'),
            hero_name: document.querySelector('.hero-text h2 .highlight'),
            hero_tagline: document.querySelector('.hero-text .tagline'),
            hero_description: document.querySelector('.hero-text p:nth-of-type(2)'),
            btn_projects: document.querySelector('.cta-buttons .btn.primary'),
            btn_contact: document.querySelector('.cta-buttons .btn.secondary'),
            
            // 关于我
            about_title: document.querySelector('#about .section-title'),
            about_role: document.querySelector('.about-summary h3'),
            about_description: document.querySelector('.about-description'),
            about_interests: document.querySelector('.about-interests'),
            about_email: document.querySelector('.about-contact-info span:nth-child(1)'),
            about_phone: document.querySelector('.about-contact-info span:nth-child(2)'),
            about_location: document.querySelector('.about-contact-info span:nth-child(3)'),
            timeline_title: document.querySelector('.timeline-section h3'),
            achievements_title: document.querySelector('.achievements-section h3'),
            
            // 技能
            skills_title: document.querySelector('#skills .section-title'),
            core_skills: document.querySelector('.skill-category:nth-child(1) h3'),
            professional_skills: document.querySelector('.skill-category:nth-child(2) h3'),
            
            // 项目
            projects_title: document.querySelector('#projects .section-title'),
            
            // 联系
            contact_title: document.querySelector('#contact .section-title'),
            contact_subtitle: document.querySelector('.contact-info h3'),
            contact_description: document.querySelector('.contact-info p'),
            contact_email: document.querySelector('.contact-method:nth-child(1) span'),
            contact_form_name: document.querySelector('label[for="name"]'),
            contact_form_email: document.querySelector('label[for="email"]'),
            contact_form_message: document.querySelector('label[for="message"]'),
            contact_form_name_placeholder: document.querySelector('#name'),
            contact_form_email_placeholder: document.querySelector('#email'),
            contact_form_message_placeholder: document.querySelector('#message'),
            contact_form_submit: document.querySelector('.contact-form button'),
            
            // 页脚
            footer_copyright: document.querySelector('footer p')
        };
    }
    
    bindEvents() {
        // 语言切换事件
        document.addEventListener('click', (e) => {
            if (e.target.closest('.language-switcher')) {
                e.preventDefault();
                const lang = e.target.dataset.lang;
                if (lang && lang !== this.currentLanguage) {
                    this.updateLanguage(lang);
                }
            }
        });
        
        // 监听DOM变化，重新绑定元素
        const observer = new MutationObserver(() => {
            setTimeout(() => this.bindElements(), 100);
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }
    
    updateLanguage(lang) {
        this.currentLanguage = lang;
        localStorage.setItem('language', lang);
        document.documentElement.lang = lang === 'zh' ? 'zh-CN' : 'en';
        
        this.applyTranslations();
        this.updateLanguageSelector();
    }
    
    applyTranslations() {
        const trans = translations[this.currentLanguage];
        
        // 应用翻译到元素
        Object.keys(this.elements).forEach(key => {
            const element = this.elements[key];
            if (element && trans[key]) {
                if (key.includes('placeholder')) {
                    element.placeholder = trans[key];
                } else if (key.includes('nav_')) {
                    element.textContent = trans[key];
                } else if (key.includes('btn_')) {
                    element.textContent = trans[key];
                } else if (key.includes('_title') || key.includes('_subtitle') || key.includes('_description') || key.includes('_interests') || key.includes('_email') || key.includes('_phone') || key.includes('_location')) {
                    if (key === 'about_email' || key === 'about_phone' || key === 'about_location' || key === 'contact_email') {
                        // 保留图标，只替换文本部分
                        const icon = element.querySelector('i');
                        if (icon) {
                            element.innerHTML = `<i class="${icon.className}"></i> ${trans[key]}`;
                        } else {
                            element.textContent = trans[key];
                        }
                    } else {
                        element.textContent = trans[key];
                    }
                } else {
                    element.textContent = trans[key];
                }
            }
        });
        
        // 更新时间线内容
        this.updateTimeline();
        this.updateAchievements();
        this.updateProjects();
        this.updateSocialLinks();
    }
    
    updateTimeline() {
        const trans = translations[this.currentLanguage];
        const timelineItems = document.querySelectorAll('.timeline-item');
        
        timelineItems.forEach((item, index) => {
            const year = item.querySelector('.timeline-year');
            const title = item.querySelector('.timeline-content h4');
            const desc = item.querySelector('.timeline-content p');
            
            if (year) year.textContent = trans[`timeline_${index + 1}_year`];
            if (title) title.textContent = trans[`timeline_${index + 1}_title`];
            if (desc) desc.textContent = trans[`timeline_${index + 1}_desc`];
        });
    }
    
    updateAchievements() {
        const trans = translations[this.currentLanguage];
        const achievementCards = document.querySelectorAll('.achievement-card');
        
        achievementCards.forEach((card, index) => {
            const title = card.querySelector('h4');
            const desc = card.querySelector('p');
            
            if (title) title.textContent = trans[`achievement_${index + 1}_title`];
            if (desc) desc.textContent = trans[`achievement_${index + 1}_desc`];
        });
    }
    
    updateProjects() {
        const trans = translations[this.currentLanguage];
        const projectCards = document.querySelectorAll('.project-card');
        
        projectCards.forEach((card, index) => {
            const title = card.querySelector('h3');
            const desc = card.querySelector('p');
            const btnDetails = card.querySelector('.btn.small');
            const btnSource = card.querySelector('.btn.small.secondary');
            
            if (title) title.textContent = trans[`project_${index + 1}_title`];
            if (desc) desc.textContent = trans[`project_${index + 1}_desc`];
            if (btnDetails) btnDetails.textContent = trans['btn_view_details'];
            if (btnSource) btnSource.textContent = trans['btn_source_code'];
        });
    }
    
    updateSocialLinks() {
        const trans = translations[this.currentLanguage];
        const socialLinks = document.querySelectorAll('.social-links a');
        
        socialLinks.forEach(link => {
            const title = link.getAttribute('title');
            if (title === '个人网页' || title === '个人网页') {
                link.setAttribute('title', trans['social_homepage']);
            } else if (title === '邮箱' || title === '邮箱') {
                link.setAttribute('title', trans['social_email']);
            }
        });

        // 更新placeholder属性
        const placeholderElements = document.querySelectorAll('[data-lang-placeholder]');
        placeholderElements.forEach(element => {
            const key = element.getAttribute('data-lang-placeholder');
            if (translations[this.currentLanguage][key]) {
                element.placeholder = translations[this.currentLanguage][key];
            }
        });
    }
    
    updateLanguageSelector() {
        const selector = document.querySelector('.language-switcher');
        if (selector) {
            const buttons = selector.querySelectorAll('button');
            buttons.forEach(btn => {
                btn.classList.toggle('active', btn.dataset.lang === this.currentLanguage);
            });
        }
    }
    
    getCurrentLanguage() {
        return this.currentLanguage;
    }
}

// 初始化语言管理器
let languageManager;

document.addEventListener('DOMContentLoaded', function() {
    languageManager = new LanguageManager();
});